﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace The_LarX
{
    public class SquareMessageBox : GuideLikeComponent
    {
        public Rectangle CenterRectangle;
        public Rectangle DestinationCenterRectangle;

        public int ScreenWidth;
        public int ScreenHeight;

        public Color BlackRect;
        public Rectangle ScreenRectangle;

        public event EventHandler OnAccepted;
        public event EventHandler OnDenied;

        private string Title;

        public Sprite2D AcceptOption;
        public Sprite2D DenyOption;
        public Sprite2D Current;

        public SquareMessageBox(string Title, string AcceptText, string DenyText)
        {
            this.Title = Title;

            this.NeedToBlockUpdate = true;            

            this.ScreenWidth = Engine.GraphicsDevice.PresentationParameters.BackBufferWidth;
            this.ScreenHeight = Engine.GraphicsDevice.PresentationParameters.BackBufferHeight;

            DestinationCenterRectangle = new Rectangle((ScreenWidth / 2) / 2, (ScreenHeight - (ScreenHeight - (ScreenHeight / 8))) / 2,
                ScreenWidth / 2, ScreenHeight - (ScreenHeight / 8));

            ScreenRectangle = new Rectangle(0, 0, ScreenWidth, ScreenHeight);

            CenterRectangle = new Rectangle(ScreenWidth / 2, ScreenHeight / 2, 0, 0);

            AcceptOption = new Sprite2D(new Vector2((ScreenWidth / 2) / 2, 0), AcceptText);
            AcceptOption.CalculateSizeRectangle = false;
            AcceptOption.RectangleColor = Color.Gray;
            AcceptOption.DrawRectangle = true;
            AcceptOption.SizeRectangle = new Rectangle((ScreenWidth / 2) / 2, (ScreenHeight - (ScreenHeight / 8)) / 2, 
                DestinationCenterRectangle.Width, DestinationCenterRectangle.Height / 4);
            DenyOption = new Sprite2D(new Vector2((ScreenWidth / 2) / 2, (ScreenHeight - (ScreenHeight / 8)) / 2), DenyText);
            DenyOption.RectangleColor = Color.Green;
            DenyOption.CalculateSizeRectangle = false;
            DenyOption.DrawRectangle = true;
            AcceptOption.SizeRectangle = new Rectangle((ScreenWidth / 2) / 2, (ScreenHeight - (ScreenHeight / 8)) / 2, 
                DestinationCenterRectangle.Width, DestinationCenterRectangle.Height / 4);
        }

        public override void Update()
        {
            base.Update();

            AcceptOption.Update();
            DenyOption.Update();

            CenterRectangle = AnimationHelper.AnimateStatic(CenterRectangle, DestinationCenterRectangle, 10);
            AcceptOption.SizeRectangle = new Rectangle((ScreenWidth / 2) / 2, (int)((ScreenHeight - (ScreenHeight / 8)) / 2.1f),
                CenterRectangle.Width, CenterRectangle.Height / 4);
            AcceptOption.Position = new Vector2(Engine.RenderManager.CenterTextX(AcceptOption.Text, "Kootenay", 1, AcceptOption.SizeRectangle),
                Engine.RenderManager.CenterTextY(AcceptOption.Text, "Kootenay", 1, AcceptOption.SizeRectangle));
            DenyOption.SizeRectangle = new Rectangle((ScreenWidth / 2) / 2, (ScreenHeight - (ScreenHeight / 8)) - CenterRectangle.Height / 4,
                CenterRectangle.Width, CenterRectangle.Height / 4);
            DenyOption.Position = new Vector2(Engine.RenderManager.CenterTextX(DenyOption.Text, "Kootenay", 1, DenyOption.SizeRectangle),
                Engine.RenderManager.CenterTextY(DenyOption.Text, "Kootenay", 1, DenyOption.SizeRectangle));

            if (Engine.Input.IsActivePlayerButtonDown(Buttons.B) ||
                Engine.Input.IsKeyboardDown(Keys.Escape))
            {
                this.Disable();
            }
            if (Engine.Input.IsActivePlayerButtonPress(Buttons.LeftThumbstickUp) ||
                Engine.Input.IsActivePlayerButtonPress(Buttons.DPadUp) ||
                Engine.Input.IsKeyboardPress(Keys.Up))
            {
                Current = AcceptOption;
            }
            if (Engine.Input.IsActivePlayerButtonPress(Buttons.LeftThumbstickDown) ||
                Engine.Input.IsActivePlayerButtonPress(Buttons.DPadDown) ||
                Engine.Input.IsKeyboardPress(Keys.Down))
            {
                Current = DenyOption;
            }

            if (Engine.Input.IsActivePlayerButtonPress(Buttons.A) ||
                Engine.Input.IsKeyboardPress(Keys.Enter))
            {
                if (Current == AcceptOption)
                {
                    if (OnAccepted != null)
                    {
                        OnAccepted(this, EventArgs.Empty);
                    }
                }
                if (Current == DenyOption)
                {
                    if (OnDenied != null)
                    {
                        OnDenied(this, EventArgs.Empty);
                    }
                    else
                    {
                        this.Disable();
                    }
                }
            }
        }

        private Color TextColor;

        public override void Draw()
        {
            base.Draw();

            BlackRect = AnimationHelper.AnimateStatic(BlackRect, new Color(0, 0, 0, 170), 10);
            Engine.RenderManager.DrawRectangle(BlackRect, ScreenRectangle, null);
            Engine.RenderManager.DrawRectangle(new Color(200, 200, 200, 255), CenterRectangle, null);
            if (CenterRectangle.Width > ScreenWidth / 2.1f)
            {
                if (AcceptOption == Current)
                {
                    AcceptOption.RectangleColor = Color.Green;
                    AcceptOption.Color = Color.White;
                    AcceptOption.Shadow = 2;
                    DenyOption.RectangleColor = Color.Gray;
                    DenyOption.Color = Color.Black;
                    DenyOption.Shadow = 0;
                }
                else
                {
                    AcceptOption.RectangleColor = Color.Gray;
                    AcceptOption.Color = Color.Black;
                    AcceptOption.Shadow = 0;
                    DenyOption.RectangleColor = Color.Green;
                    DenyOption.Color = Color.White;
                    DenyOption.Shadow = 2;
                }
                TextColor = AnimationHelper.AnimateStatic(TextColor, Color.White, 10);
                Engine.RenderManager.DrawString(Engine.RenderManager.DivideString(Title, CenterRectangle.Width - 50, "Kootenay"),
                    2, TextColor.A, new Vector2(CenterRectangle.X + 25, CenterRectangle.Y + 25), "Kootenay",
                    TextColor, null);
                AcceptOption.Draw();
                DenyOption.Draw();
            }
        }
    }
}
